package Demo7;

public class Demo7 {
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();

    public static void main(String[] args) {

        // 线程 1 先获取 lock1 再获取 lock2
        Thread thread1 = new Thread(() -> {
            synchronized (lock1) {
                System.out.println("线程1得到了锁lock1");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1尝试获取lock2......");
                synchronized (lock2) {
                    System.out.println("线程1获取到了lock2");
                }
            }
        });

        // 线程 2 先获取 lock2 再获取 lock1
        Thread thread2 = new Thread(() -> {
            synchronized (lock2) {
                System.out.println("线程2得到了锁lock2");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程2尝试获取lock1......");
                synchronized (lock1) {
                    System.out.println("线程2获取到了lock1");
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}